﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Globalization;
using System.ComponentModel.Design;
using System.Reflection;
using System.Diagnostics;

namespace ProgNetComponents.Design.TypeConverters
{
    public class StringToTypeConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string))
                return true;
            return base.CanConvertFrom(context, sourceType);
        }
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string)
            {
                try
                {
                    return  context.GetType(value.AsString());
                }
                catch { }
            }
            return base.ConvertFrom(context, culture, value);
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                if (value is Type)
                {
                    return ((Type)(value)).FullName;
                }
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }

    }
}
